package view.components;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.print.attribute.standard.JobHoldUntil;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.tree.DefaultMutableTreeNode;

import com.sun.xml.internal.messaging.saaj.soap.ver1_1.Header1_1Impl;

import view.listeners.IpAddressTableListener;

import model.HostAddress;
import model.IPAddress;
import model.Subnet;
import model.TableModel;

import controller.Controller;

public class IpAddressTable extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JTable ipTable;
	private Controller controller = Controller.getInstance();
	private final TableModel model;
	

	public TableModel getModel() {
		return model;
	}

	private NetworkTree networkTree;
	private Subnet actualSubnet;

	public void setNetworkTree(NetworkTree networkTree) {
		this.networkTree = networkTree;
	}

	public IpAddressTable() {
		String[] titles = new String[] { "IP-Adresse", "Infotext (Alle)" };
		model = new TableModel(titles, 0);
	
		ipTable = new JTable(model);
		
		 TableColumnModel model = ipTable.getColumnModel();
		
		 final JTableHeader header = ipTable.getTableHeader();
		  header.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0) {
				TableColumn column = header.getColumnModel().getColumn(1);
				 boolean isSet = false;
				if (column.getHeaderValue().toString().equalsIgnoreCase("Infotext (Alle)") && !isSet){
					column.setHeaderValue("Infotext (Nur freie)");
					isSet = true;
					clearTable();
					loadTable(networkTree.getSubnetFromAddress(networkTree.getSelectedNode().toString()), 1);
				}
				if (column.getHeaderValue().toString().equalsIgnoreCase("Infotext (Nur freie)") && !isSet){
					column.setHeaderValue("Infotext (Nur vergebene)");
					isSet = true;
					clearTable();
					loadTable(networkTree.getSubnetFromAddress(networkTree.getSelectedNode().toString()), 2);
				
				}
				if (column.getHeaderValue().toString().equalsIgnoreCase("Infotext (Nur vergebene)") && !isSet){
					column.setHeaderValue("Infotext (Alle)");
					isSet = true;
					clearTable();
					loadTable(networkTree.getSubnetFromAddress(networkTree.getSelectedNode().toString()), 0);
				
				}
				
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		ipTable.setEnabled(true);

		ipTable.getColumnModel().getColumn(0).setPreferredWidth(4);

		JScrollPane pane = new JScrollPane(ipTable);
		pane.setPreferredSize(new Dimension(520, 320));
		// pane.setLayout(new GridLayout(1,1));
		this.setLayout(new GridLayout(1, 1));
		this.add(pane);

		ipTable.addMouseListener(new IpAddressTableListener(this));
	}

	public void addIpAdress(String ipAdress, String infoText) {
		String subnet = networkTree.getSelectedNode().toString();
		ArrayList<HostAddress> hostAdressList = controller.getHosts();
		for (int i = 0; i < hostAdressList.size(); i++) {
			for (int y = 0; y < hostAdressList.get(i).getSubnetList().size(); y++) {
				if (hostAdressList.get(i).getSubnetList().get(y).getNetAdress()
						.getAddress() == subnet) {
					hostAdressList.get(i).getSubnetList().get(y)
							.getIpAddressList()
							.add(new IPAddress(ipAdress, infoText));
					controller.setHosts(hostAdressList);
				}
			}
		}

	}

	public int getSelectedRow() {
		return ipTable.getSelectedRow();
	}

	public String getSelectedIPAdress() {
		String ipAdress = model.getValueAt(getSelectedRow(), 0).toString();

		return ipAdress;

	}

	public void clearTable() {
		model.removeAll();
	}

	public void setIpAddressEmpty(String ipAddress) {
		ArrayList<HostAddress> hosts = Controller.getInstance().getHosts();
		for (HostAddress host : hosts) {
			for (Subnet subnet : host.getSubnetList()) {
				if (subnet.equals(actualSubnet)) {
					for (IPAddress ip : subnet.getIpAddressList()) {
						if (ip.getAddress().equals(ipAddress)) {
							ip.setAdditionalInfo("<Free>");
						}
					}

				}

			}

		}
		clearTable();
		loadTable(actualSubnet, 0);
	}

	public void loadTable(Subnet subnet, int modus) {
		actualSubnet = subnet;
		
		if (modus == 0) {
			allIPs(subnet);
		}
		if (modus == 1) {
			freeIPs(subnet);
		}
		if (modus == 2) {
			assignedIPs(subnet);
		}
	}

	private void assignedIPs(Subnet subnet) {
		for (IPAddress ip : subnet.getIpAddressList()) {
			if (ip.getAdditionalInfo().equalsIgnoreCase("<Free>")) {
				addRow(ip);
			}
		}
	}

	private void freeIPs(Subnet subnet) {
		for (IPAddress ip : subnet.getIpAddressList()) {
			if (ip.getAdditionalInfo().equalsIgnoreCase("<Free>")) {
				addRow(ip);
			}
		}
	}

	private void addRow(IPAddress ip) {
		Vector<String> newData = new Vector<String>();
		newData.addElement(ip.getAddress());
		newData.addElement(ip.getAdditionalInfo());
		model.addRow(newData);
	}

	private void allIPs(Subnet subnet) {
		for (IPAddress ip : subnet.getIpAddressList()) {
			addRow(ip);

		}
	}
	
	class ButtonHeaderRenderer extends JButton implements TableCellRenderer {
	    int pushedColumn;

	    public ButtonHeaderRenderer() {
	      pushedColumn = -1;
	      setMargin(new Insets(0, 0, 0, 0));
	    }

	    public Component getTableCellRendererComponent(JTable table,
	        Object value, boolean isSelected, boolean hasFocus, int row,
	        int column) {
	      setText((value == null) ? "" : value.toString());
	      boolean isPressed = (column == pushedColumn);
	      getModel().setPressed(isPressed);
	      getModel().setArmed(isPressed);
	      return this;
	    }

	    public void setPressedColumn(int col) {
	      pushedColumn = col;
	    }
	  }

}
